﻿using FastCloud.Base.Desgin;
using FastIOC.Attribute;
using FastUtil.Config;
using FastUtil.Convert;
using FastUtil.Extra;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastCloud.Base.Config
{
    [Component]
    public class TokenUtil
    {
        private string TokenPrefix = AppConfigUtil.Configuration["Redis:TokenPrefix"];
        private string SessionTimeOut = AppConfigUtil.Configuration["Redis:SessionTimeOut"];

        public string CreateToken(string RealUserGuid,string UserGuid, string LoginID, string DisplayName, string Mobile, string OUGuid, string OUName, string OUCode, string ParentOUGuid)
        {
            string Token = TokenPrefix + "_" + Guid.NewGuid().ToString();
            RedisUtil.SetHash(Token, "RealUserGuid", RealUserGuid);
            RedisUtil.SetHash(Token, "LoginID", LoginID);
            RedisUtil.SetHash(Token, "UserGuid", UserGuid);
            RedisUtil.SetHash(Token, "DisplayName", DisplayName);
            RedisUtil.SetHash(Token, "Mobile", Mobile);
            RedisUtil.SetHash(Token, "OUGuid", OUGuid);
            RedisUtil.SetHash(Token, "OUName", OUName);
            RedisUtil.SetHash(Token, "OUCode", OUCode);
            RedisUtil.SetHash(Token, "ParentOUGuid", ParentOUGuid);
            RedisUtil.SetKeyExpire(Token, TimeSpan.FromMinutes(ValueConvert.ConvertToInt32(SessionTimeOut)));
            //同步Session
            CloudContext.Current.Session.SetString("Token", Token);
            return Token;
        }

        public  void UpdateToken(string UserGuid, string LoginID, string DisplayName, string Mobile, string OUGuid, string OUName, string OUCode, string ParentOUGuid)
        {
            string Token = GetToken();
            RedisUtil.SetHash(Token, "LoginID", LoginID);
            RedisUtil.SetHash(Token, "UserGuid", UserGuid);
            RedisUtil.SetHash(Token, "DisplayName", DisplayName);
            RedisUtil.SetHash(Token, "Mobile", Mobile);
            RedisUtil.SetHash(Token, "OUGuid", OUGuid);
            RedisUtil.SetHash(Token, "OUName", OUName);
            RedisUtil.SetHash(Token, "OUCode", OUCode);
            RedisUtil.SetHash(Token, "ParentOUGuid", ParentOUGuid);
            RedisUtil.SetKeyExpire(Token, TimeSpan.FromMinutes(ValueConvert.ConvertToInt32(SessionTimeOut)));
        }

        public string GetToken()
        {
            if (CloudContext.Current!=null)
            {
                string Token = CloudContext.Current.Session.GetString("Token");
                if (string.IsNullOrEmpty(Token))
                {
                    Token = CloudUtil.GetFormParam("Token");
                }
                if (string.IsNullOrEmpty(Token))
                {
                    Token = CloudUtil.GetUrlParam("Token");
                }
                if (string.IsNullOrEmpty(Token))
                {
                    Token = ValueConvert.ConvertToString(CloudContext.Current.Session.GetString("Token"));
                }
                return Token;
            }
            else
            {
                return string.Empty;
            }
        }
        public bool ValidToken()
        {
            string Token = GetToken();
            bool Valid = true;
            try
            {
                if (!RedisUtil.ExistKey(Token))
                {
                    Valid = false;
                }
                if (!RedisUtil.ExistHash(Token, "UserGuid"))
                {
                    Valid = false;
                }

                string UserGuid = RedisUtil.GetHash(Token, "UserGuid");
                if (UserGuid == null || string.IsNullOrEmpty(UserGuid))
                {
                    Valid = false;
                }
            }
            catch (Exception e)
            {

                Valid = false;
            }
            return Valid;
        }
        public void RefreshToken()
        {
            string Token = GetToken();
            RedisUtil.SetKeyExpire(Token, TimeSpan.FromMinutes(Convert.ToDouble(SessionTimeOut)));
        }
        public void RemoveToken()
        {
            string Token = GetToken();
            RedisUtil.DelteKey(Token);
        }

        public TokenEntity GetTokenEntity()
        {
            TokenEntity Entity = new TokenEntity();
            string Token = GetToken();
            if (!string.IsNullOrEmpty(Token))
            {
                Entity.RealUserGuid = RedisUtil.GetHash(Token, "RealUserGuid");
                Entity.LoginID = RedisUtil.GetHash(Token, "LoginID");
                Entity.UserGuid = RedisUtil.GetHash(Token, "UserGuid");
                Entity.DisplayName = RedisUtil.GetHash(Token, "DisplayName");
                Entity.Mobile = RedisUtil.GetHash(Token, "Mobile");
                Entity.OUGuid = RedisUtil.GetHash(Token, "OUGuid");
                Entity.OUName = RedisUtil.GetHash(Token, "OUName");
                Entity.OUCode = RedisUtil.GetHash(Token, "OUCode");
                Entity.ParentOUGuid = RedisUtil.GetHash(Token, "ParentOUGuid");
            }
            return Entity;
        }

        /// <summary>
        /// 是否兼职登陆
        /// </summary>
        /// <returns></returns>
        public bool IsLoginAsUserJob()
        {
            TokenEntity Entity = GetTokenEntity();
            return Entity.RealUserGuid == Entity.UserGuid;
        }

    }
}
